<?php
class AdminNewsController  extends Zone_Action{
	public function fields(){
		$data = array(
			title 	=> array(
				type 		=> 'CHAR',
				form_label  => 'Tiêu đề',			
				no_empty	=> true
			),
			ord	=> array(
				type			=> 'INT',
				default_value	=> new Model_Expr("LAST_INSERT_ID()+1")
			),
			date_updated	=> array(
				type			=> 'NONE',
				default_value	=> new Model_Expr("NOW()")
			),
			updated_by_id =>array(
				type			=> 'NONE',
				default_value	=> get_user_id()
			)
		);
		return $data;	
	}
	
	public function indexAction(){			
		$posts = $this->Model->fetchAll("SELECT `a`.*,
				`b`.`username` as `created_by_name`,
				`b`.`is_deleted` as `created_is_deleted`,
				`c`.`username` as `updated_by_name`,
				`c`.`is_deleted` as `updated_is_deleted`
			FROM `news_cats` as `a` 
			LEFT JOIN `users` as `b` 
				ON `a`.`created_by_id`=`b`.`ID`
			LEFT JOIN `users` as `c` 
				ON `a`.`updated_by_id`=`c`.`ID`
			ORDER BY `a`.`ord`");		
		$this->set('posts',$posts);	
	}
	
	public function addAction(){
		$this->removeLayout();		
		if(isPost()){
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->fields());
			$form->addFields(array(
				date_created	=> array(
					type			=> 'NONE',
					default_value	=> mysql_date()
				),
				created_by_id =>array(
					type			=> 'NONE',
					default_value	=> get_user_id()
				)	
			));			
			
			$data = $form->getData();
			if( !is_array($data) ){
				return $this->setJSON(array(
					message => error($data)
				));
			}	
			
			$this->Model->insert("news_cats",$data);
			
			return $this->setJSON(array(
				redirect => "#Admin/News"
			));
		}
	}
	
	public function editAction(){
		$this->removeLayout();		
		$post_id = getInt('ID',0);		
		$post = $this->Model->fetchRow("SELECT * FROM `news_cats` WHERE `ID`='$post_id'");
		
		if( !$post ){
			return $this->setJSON(array(
				content => error('Dữ liệu đã bị xóa hoặc không tồn tại.')
			));		
		}
		
		$this->set('post',$post);		
		if(isPost()){
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->fields($post));
			$data = $form->getData();
		
			if( !is_array($data)){
				return $this->setJSON(array(
					message => error($data)
				));
			}		
						
			$this->Model->update("news_cats",$data,"`ID`='$post_id'");
			return $this->setJSON(array(
				redirect	=> "#Admin/News"
			));
		}
	}	

	public function deleteAction(){
		$post_id = getInt("ID",0);
	
		$this->Model->delete("news_cats"," `ID`='$post_id' ");		
		return $this->setJSON(array(
			redirect	=> "#Admin/News"
		));
	}
}
?>